/*
 * @(#)FeeDetaiItemAdapter.java 创建于 2016-07-18 14:15:34
 *
 * 版权：版权所有 Bsoft 保留所有权力。
 */
package hcn.protocol.ods.diagnosis;

import ctd.util.xml.jaxb.DateTimeAdapter;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
import java.util.List;

/**
 * @author <a href="mailto:chinnsii@163.com">zhengshi</a>
 */
class FeeDetailRecordAdapter extends XmlAdapter<FeeDetailRecordAdapter.Adapted, AbstractFeeDetailRecordBean> {

    /**
     * @param v
     * @return
     * @throws Exception
     */
    @Override
    public AbstractFeeDetailRecordBean unmarshal(Adapted v) throws Exception {
        AbstractFeeDetailRecordBean bean = null;
        switch (v.feeTypeCode) {
            case "21":
            case "22":
                bean = new WesternMedicineFeeDetailRecordBean();
                setCommonProperties(bean, v);
                break;
            case "23":
                bean = new ChineseMedicineFeeDetailRecordBean();
                setCommonProperties(bean, v);
                ((ChineseMedicineFeeDetailRecordBean) bean).setQuantity(v.quantity);
                ((ChineseMedicineFeeDetailRecordBean) bean).setDecoctionDesc(v.decoctionDesc);
                ((ChineseMedicineFeeDetailRecordBean) bean).setUsage(v.usage);
                break;
            case "41":
                bean = new ExamFeeDetailRecordBean();
                setCommonProperties(bean, v);
                break;
            case "31":
            case "11":
            case "51":
            case "61":
                bean = new LabFeeDetailRecordBean();
                setCommonProperties(bean, v);
                break;
            default:
                throw new Exception("当feeCodeType="+v+",feeCodeType在HCN_feeType字典中不匹配");
        }

        return bean;
    }

    /**
     * @param bean
     * @return
     * @throws Exception
     */
    @Override
    public FeeDetailRecordAdapter.Adapted marshal(AbstractFeeDetailRecordBean bean) throws Exception {
        if (bean == null) {
            return null;
        }
        Adapted adapted = new Adapted();
        adapted.feeName = bean.getFeeName();
        adapted.feeTypeCode = bean.getFeeTypeCode();
        adapted.feeType = bean.getFeeType();
        adapted.deptId = bean.getDeptId();
        adapted.deptName = bean.getDeptName();
        adapted.feeType = bean.getFeeType();
        adapted.details = bean.getDetails();
        adapted.doctorId = bean.getDoctorId();
        adapted.doctorName = bean.getDoctorName();
        adapted.feeDate = bean.getFeeDate();
        adapted.feeDesc = bean.getFeeDesc();
        adapted.feeNo = bean.getFeeNo();
        adapted.totalFee = bean.getTotalFee();

        switch (bean.getFeeTypeCode()) {
            case "23":
                adapted.quantity = ((ChineseMedicineFeeDetailRecordBean) bean).getQuantity();
                adapted.decoctionDesc = ((ChineseMedicineFeeDetailRecordBean) bean).getDecoctionDesc();
                adapted.usage = ((ChineseMedicineFeeDetailRecordBean) bean).getUsage();
                break;
            case "21":
            case "22":
            case "41":
            case "31":
            case "11":
            case "51":
            case "61":
        }
        return adapted;
    }

    /**
     * @param bean
     * @param adapted
     * @param <T>
     */
    private <T extends AbstractFeeDetailItemBean> void setCommonProperties(AbstractFeeDetailRecordBean<T> bean, Adapted<T> adapted) {
        bean.setFeeName(adapted.feeName);
        bean.setDeptId(adapted.deptId);
        bean.setDeptName(adapted.deptName);
        bean.setDetails(adapted.details);
        bean.setDoctorId(adapted.doctorId);
        bean.setDoctorName(adapted.doctorName);
        bean.setFeeDate(adapted.feeDate);
        bean.setFeeDesc(adapted.feeDesc);
        bean.setFeeNo(adapted.feeNo);
        bean.setFeeType(adapted.feeType);
        bean.setFeeTypeCode(adapted.feeTypeCode);
        bean.setTotalFee(adapted.totalFee);
    }

    @XmlType(namespace = "feeDetailRecord")
    public static class Adapted<T extends AbstractFeeDetailItemBean> {

        @XmlElement
        private String feeName;
        @XmlElement
        private String feeType;
        @XmlElement
        private String feeNo;
        @XmlElement
        private String feeTypeCode;
        @XmlElement
        private String feeDesc;
        @XmlElement
        @XmlJavaTypeAdapter(DateTimeAdapter.class)
        private Date feeDate;
        @XmlElement
        private String totalFee;
        @XmlElement
        private String deptId;
        @XmlElement
        private String deptName;
        @XmlElement
        private String doctorId;
        @XmlElement
        private String doctorName;
        @XmlElement
        private int quantity;
        @XmlElement
        private String decoctionDesc;
        @XmlElement
        private String usage;
        @XmlElementWrapper(name = "details")
        @XmlElement(name = "detail")
        private List<T> details;

    }
}
